package com.ts.system.controller;

import com.ts.comm.SysJson;
import com.ts.comm.SysResult;
import com.ts.comm.SysString;
import com.ts.system.dao.PostDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xhc on 2017/5/2.
 */
@Controller
public class PostController {

    @Resource
    private PostDao postDao;

    /**
     * 查询岗位信息
     * */
    @RequestMapping(value = "/post/postList",method = RequestMethod.POST)
    @ResponseBody
    //@RequiresPermissions(value = {"Y104:"+ OpType.View})
    public SysResult queryData(HttpServletRequest req){
        String sWhere= SysString.toString(req.getParameter("where"));
        SysResult sr = postDao.query(sWhere);
        return sr;
    }

    /**
     * 根据fcode查询岗位信息
     * @param req
     * @return
     */
    @RequestMapping(value = "/post/getPostInfo",method = RequestMethod.POST)
    @ResponseBody
    public SysResult getPostInfo(HttpServletRequest req,@RequestBody String code){
        HashMap<String,Object> map = SysJson.toHashMap(code);
        SysResult sr = postDao.getPostInfoByFcode((String) map.get("fcode"));
        return sr;
    }

    /**
     * 添加岗位信息
     * @param request
     * @param response
     * @param code
     * @return
     */
    @RequestMapping(value = "/post/addPost",method = RequestMethod.POST)
    @ResponseBody
    public SysResult addPost(HttpServletRequest request, HttpServletResponse response, @RequestBody String code){
        HashMap<String,Object> map = SysJson.toHashMap(code);
        SysResult sysResult = postDao.add(map);
        return sysResult;
    }

    /**
     * 删除岗位信息
     * @param request
     * @param response
     * @param code
     * @return
     */
    @RequestMapping(value = "/post/deletePost",method = RequestMethod.POST)
    @ResponseBody
    public SysResult deletePost(HttpServletRequest request, HttpServletResponse response, @RequestBody String code){
        List list =  SysJson.toList(code,String.class);
        SysResult sysResult = postDao.delete(list);
        return sysResult;
    }

    /**
     * 设置岗位
     * @param request
     * @param response
     * @param code
     * @return
     */
    @RequestMapping(value = "/post/editPost",method = RequestMethod.POST)
    @ResponseBody
    public SysResult editPost(HttpServletRequest request, HttpServletResponse response, @RequestBody String code){
        HashMap<String,Object> map = SysJson.toHashMap(code);

        SysResult sysResult = postDao.update(map, (String) map.get("fCode"));
        return sysResult;
    }

    /**
     * 获取权限列表
     * @return
     */
    @RequestMapping(value = "/post/getRightList",method = RequestMethod.POST)
    @ResponseBody
    public SysResult getRightList(){
        SysResult sysResult = postDao.getRightList();
        return sysResult;
    }

    /**
     * 获取用户权限列表
     * @param request
     * @param code
     * @return
     */
    @RequestMapping(value = "/post/getPostRightList",method = RequestMethod.POST)
    @ResponseBody
    public SysResult getPostRightList(HttpServletRequest request,@RequestBody String code){
        HashMap<String,Object> map = SysJson.toHashMap(code);
        String postCode = (String)map.get("postCode");
        SysResult sysResult = postDao.getPostRightList(postCode);
        return sysResult;
    }

    /**
     * 设置岗位权限
     * @param request
     * @param code
     * @return
     */
    @RequestMapping(value = "/post/setPostRight")
    @ResponseBody
    public SysResult setPostRight(HttpServletRequest request,@RequestBody String code){
        Map map = SysJson.toMap(code);
        SysResult sysResult = postDao.setPostRight(map);
        return sysResult;
    }

}
